﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSQuyetDinh : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string GetMaSo(int qdID)
        {
            return this.Context.NSQuyetDinhs.Where(a => a.ID.Equals(qdID)).SingleOrDefault().MaQD;
        }
        /// <summary>
        /// Insert Quyết định
        /// </summary>
        /// <param name="loaiQD"></param>
        /// <param name="ngayQD"></param>
        /// <param name="path"></param>
        /// <param name="ghiChu"></param>
        /// <param name="maSo"></param>
        public int InsertQuyetDinh(int loaiQD, DateTime ngayQD, string ghiChu, string maSo, string fileName, string fileTitle)
        {
            NSQuyetDinh item = new NSQuyetDinh();
            item.LoaiQuyetDinh = loaiQD;
            if (!(ngayQD.Year == 1))
            {
                item.NgayQD = ngayQD;
            }

            item.MaQD = maSo;
            item.PathFileQD = fileName;
            item.GhiChu = ghiChu;
            item.TitleFile = fileTitle;
            db.NSQuyetDinhs.InsertOnSubmit(item);
            db.SubmitChanges();
            return item.ID;
        }
        /// <summary>
        /// Sửa quyết định
        /// </summary>
        /// <param name="QDID"></param>
        /// <param name="loaiQD"></param>
        /// <param name="ngayQD"></param>
        /// <param name="path"></param>
        /// <param name="ghiChu"></param>
        /// <param name="maSo"></param>
        public void Edit(int QDID, int loaiQD, DateTime ngayQD, string path, string ghiChu, string maSo)
        {
            NSQuyetDinh item = this.db.NSQuyetDinhs.Where(a => a.ID.Equals(QDID)).SingleOrDefault();
            if (item != null)
            {
                item.LoaiQuyetDinh = loaiQD;
                if (!(ngayQD.Year == 1))
                {
                    item.NgayQD = ngayQD;
                }
                item.MaQD = maSo;
                item.PathFileQD = path;
                item.GhiChu = ghiChu;
                db.SubmitChanges();
            }

        }
        /// <summary>
        /// Delete quyết định
        /// </summary>
        /// <param name="quyetDinhId"></param>
        public void DeleteQuyetDinh(int quyetDinhId)
        {
            var query = (from a in db.NSQuyetDinhs
                         where a.ID.Equals(quyetDinhId)
                         select a).SingleOrDefault();

            db.NSQuyetDinhs.DeleteOnSubmit(query);
            db.SubmitChanges();
        }
        public NSQuyetDinh GetQuyetDinhByID(int quyetDinhId)
        {
            return this.db.NSQuyetDinhs.Where(a => a.ID.Equals(quyetDinhId)).SingleOrDefault();
        }
    }
}